﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Comissoes.Models;
using Comissoes.JQGridObject;

namespace Comissoes.Controllers
{
    public class CnaesController : AppController
    {
        xcontEntities db = new xcontEntities();

        // Monta a lista de tipos de produtos
        public JsonResult List(String sidx, String sord, Int32 page, Int32 rows, Boolean _search, String searchField, String searchString, String searchOper, Int32? COD_LOJA)
        {
            try
            {
                var q = from c in db.CNAES.ToList()
                        where c.SUBCLASSE_CNAE.HasValue
                        select new
                        {
                            c.ID_CNAE,
                            COD_CNAE = c.DIVISAO_CNAE.ToString().PadLeft(2, '0') + c.GRUPO_CNAE.ToString().PadLeft(2, '0') + "-" + c.CLASSE_CNAE + "/" + c.SUBCLASSE_CNAE.ToString().PadLeft(2, '0'),
                            c.DESC_CNAE
                        };

                JQGridBuilder jbuilder = new JQGridBuilder(q.AsQueryable(), page, sidx, sord, searchField, searchString, searchOper, "ID_CNAE", _search, rows);

                return Json(jbuilder.GetObject(), JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(null, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
